package cn.wolfcode.trip.web.controller;

import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/strategyCatalog")
public class StrategyCatalogController {
    @Autowired
    private IStrategyCatalogService service;
    @Autowired
    private IStrategyService strategyService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyCatalogQueryObject qo) {
        qo.setOrderBy("sc.strategy_id,sc.sequence");
        PageInfo<StrategyCatalog> pageInfo = service.selectList(qo);
        model.addAttribute("result", pageInfo);
        model.addAttribute("strategies", strategyService.selectEnable());

        return "strategyCatalog/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(StrategyCatalog strategyCatalog) {
        service.saveOrUpdate(strategyCatalog);
        return "redirect:/strategyCatalog/list.do";
    }

    @RequestMapping("/enable")
    @ResponseBody
    public Object getStrateCatalogByStrateId(Long strateId) {
        List<StrategyCatalog> list = service.selectListByStrateId(strateId);
        return list;
    }
    @RequestMapping("changState")
    @ResponseBody
    public Object changState(Long id,boolean state){
        JSONResult json = new JSONResult();
        try {
            service.updateStateById(id,state);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark("操作失败");
        }
        return json;
    }

}
